using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WebPaint
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Image1.ImageUrl = "getImage.aspx?userId=" + Session.SessionID + "&id=1&type=0";
            Label1.Text = "Id. Usuario: " + Session.SessionID +
                "<br />TimeOut: " + Session.Timeout.ToString();
        }

        protected void subirBtn_Click(object sender, EventArgs e)
        {
            string id = System.Guid.NewGuid().ToString();
            FileUpload1.SaveAs(
                ConfigurationManager.AppSettings["PNETDirTemp"] +
                Session.SessionID + "\\" + id);

            string file = ConfigurationManager.AppSettings["PNETDirTemp"] +
                Session.SessionID + "\\" + id;

            Stream imageStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);

            JpegBitmapDecoder decoder = new JpegBitmapDecoder(imageStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);

            BitmapSource bitmapSource = decoder.Frames[0];

            BitmapMetadata Mdata = (BitmapMetadata)bitmapSource.Metadata;

            this.Title = Mdata.Copyright;

            Image1.ImageUrl = "getImage.aspx?userId=" + Session.SessionID + "&id=" + id + "&type=0";
        }   
        Bitmap BSourceToBitmap(BitmapSource BS)
        {
            int stride = BS.PixelWidth * BS.Format.BitsPerPixel / 8;
            stride = stride + (stride % 4) * 4;
            Byte[] Pixels = new Byte[stride * BS.PixelHeight * BS.Format.BitsPerPixel / 8];
            BS.CopyPixels(Pixels, stride, 0);

            unsafe
            {
                fixed (byte* pPixels = Pixels)
                {
                    IntPtr ptr = new IntPtr(pPixels);
                    Bitmap bitmap = new Bitmap(
                        BS.PixelWidth,
                        BS.PixelHeight,
                        stride,
                        System.Drawing.Imaging.PixelFormat.Format24bppRgb,
                        ptr);
                    return bitmap;
                }
            }

        }

    }
}
